home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / DataItem.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  9.5 KB  |  278 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DataItem.h
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __DATAITEM__
  15. #define __DATAITEM__
  16.  
  17. #ifndef __MEMORY__
  18. #include "Memory.h"
  19. #endif
  20.  
  21. #ifndef    __DIRECTOBJECT__
  22. #include "DirectObject.h"
  23. #endif
  24.  
  25. #ifndef __APPLEEVENTS__
  26. //    For DescType and typedefs for data types
  27. #include <AppleEvents.h>
  28. #endif
  29.  
  30. /***********************************|****************************************/
  31.  
  32. #pragma push
  33. #pragma segment DataItem
  34.  
  35. class ostream;
  36. class ADataItem;
  37.  
  38. extern short CompareDataItems ( const void* a, unsigned long alen, const void* b, unsigned long blen );
  39.  
  40. /*============================================================================
  41.  
  42.     ADataItem is a abstract class representing a DataItem, a chunk of
  43.     contiguous RAM which has a physical length and a starting address
  44.     and a 'type'.
  45.  
  46. ----------------------------------------------------------------------------*/
  47.  
  48. class ADataItem : public TDirectObject
  49. {
  50. public:
  51.  
  52.     virtual    ~ADataItem ();
  53.             ADataItem&                operator = ( const ADataItem& );
  54.  
  55. //    Conversion operators, which allow simple assignments to be made to
  56. //    a DataItem like:
  57. //        ADataItem x;
  58. //        x = 7;    // x.GetDataType() == typeLongint, * ( long ) x.GetPhysicalStart() = 8
  59. //        x = "Hi!"; // x.GetDataType() == typeText; x.GetPhysicalStart() = "Hi!";
  60. //        et al.
  61. //
  62.     virtual    ADataItem&                operator = ( const long );
  63.     virtual    ADataItem&                operator = ( const char* );
  64.     virtual    ADataItem&                operator = ( const StringPtr );
  65.             
  66.  
  67.     virtual                            operator long () const;
  68.     virtual                             operator Str255& () const;
  69.  
  70.                                     operator const void* () const;
  71.  
  72. // subclasses must implement these…
  73.     virtual    const void*                GetPhysicalStart () const = 0;
  74.     virtual    unsigned long            SetPhysicalLength ( unsigned long ) = 0;
  75.     virtual    unsigned long            GetPhysicalLength () const = 0;
  76.     virtual DescType                GetDataType() const = 0;
  77.     virtual void                    SetDataType(DescType dataType) = 0;
  78.  
  79. // comparison methods
  80.     virtual    Boolean                    operator == ( const ADataItem& ) const;
  81.     virtual    Boolean                    operator != ( const ADataItem& ) const;
  82.  
  83. // safe copying methods
  84.     virtual    unsigned long            ReadFrom ( const void* source, unsigned long length, DescType dataType);
  85.     virtual    unsigned long            WriteTo ( void* dest, unsigned long length ) const;
  86.  
  87. // content setting methods
  88.     virtual    void                    ZeroDataItem ();
  89.     virtual    void                    FillDataItem ( unsigned char );
  90.  
  91. // debugging
  92.     virtual    ostream&                operator >> ( ostream& ) const;
  93.  
  94. protected:    ADataItem ();
  95. };
  96.  
  97. /*============================================================================
  98.  
  99.     CDataItem is a concrete class representing a DataItem.
  100.  
  101.     The implementation of this class is smart; i.e. if the DataItem logical
  102.     length is no greater than 7 bytes, then the DataItem data is stored
  103.     inside the object, that keeps us from senselessly allocating teensy
  104.     blocks on the heap since we have space in the object already.
  105.  
  106. ----------------------------------------------------------------------------*/
  107.  
  108. class CDataItem : public ADataItem
  109. {
  110. public:        CDataItem ();
  111.             CDataItem ( unsigned long length, DescType dataType = typeWildCard );
  112.             CDataItem ( const void* source, unsigned long length, DescType dataType = typeWildCard );
  113.             CDataItem ( const ADataItem& );
  114.             CDataItem ( const CDataItem& );
  115.     virtual    ~CDataItem ();
  116.             CDataItem&                operator = ( const CDataItem& );
  117.  
  118.     virtual    const void*                GetPhysicalStart () const;
  119.     virtual    unsigned long            SetPhysicalLength ( unsigned long );
  120.     virtual    unsigned long            GetPhysicalLength () const;
  121.  
  122.     virtual DescType                GetDataType() const;
  123.     virtual void                    SetDataType(DescType dataType);
  124.     
  125.     virtual    ostream&                operator >> ( ostream& ) const;
  126.  
  127. private:
  128.             unsigned char            fIsExternal;
  129.             DescType                fDataType;
  130.             
  131.             struct DataItemExternal
  132.             {
  133.                 unsigned long        fLength;
  134.                 void*                fDataItem;
  135.             };
  136.  
  137. #define    kDataItemMaxInternalLength ( sizeof ( DataItemExternal ) -  sizeof ( unsigned char ) )
  138.  
  139.             struct DataItemInternal
  140.             {
  141.                 unsigned char        fLength;
  142.                 unsigned char        fDataItem [ kDataItemMaxInternalLength ];
  143.             };
  144.  
  145.             union
  146.             {
  147.                 DataItemExternal    fExternal;
  148.                 DataItemInternal    fInternal;
  149.             } fType;
  150. };
  151.  
  152. /*============================================================================
  153.  
  154.     CPrefixDataItem is a concrete class which represents a DataItem
  155.     which creates a new DataItem containing a length prefix from an existing
  156.     DataItem. The length prefix can be a 1, 2, or 4 byte unsigned integer.
  157.  
  158.     The length prefix DataItem will be larger than the original DataItem by
  159.     either 1, 2, or 4 bytes, and this prefix will contain an unsigned
  160.     integer representing the length of the original DataItem.
  161.  
  162.     When the length DataItem is destructed, it can also write its contents
  163.     back to the original DataItem, this option is selected in the constructor
  164.     argument list parameter “updateSource”.
  165.  
  166.     For example, this class can be used to easily create a pascal string
  167.     copy of a null terminated string:
  168.  
  169.         char s [] = "Hello World!!";
  170.         CPrefixDataItem b ( s, strlen ( s ), sizeof ( char ) );
  171.         char* t = (char*) b.GetPhysicalStart ();
  172.  
  173.     The data pointed to by <t> would look like:
  174.  
  175.          $0C  $48 $65 $6c $6c $6f $20 $57 $6f $72 $6c $64 $21 $21
  176.          ‘\n’ ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘!’ ‘!’
  177.  
  178.     Prefix buffers always have dataType == typeWildCard.
  179.  
  180. ----------------------------------------------------------------------------*/
  181.  
  182. class CPrefixDataItem : public ADataItem
  183. {
  184. public:
  185.  
  186.     enum DataItemPrefix { kByte = 1, kWord = 2, kLong = 4 };
  187.  
  188.             CPrefixDataItem ( const ADataItem&, DataItemPrefix = kByte );
  189.             CPrefixDataItem ( ADataItem&, Boolean update, DataItemPrefix = kByte );
  190.             CPrefixDataItem ( const void*, unsigned long, DataItemPrefix = kByte );
  191.             CPrefixDataItem ( DataItemPrefix, unsigned long logicalSize );
  192.     virtual    ~CPrefixDataItem ();
  193.  
  194.     virtual    const void*                GetLogicalStart () const;
  195.     virtual    unsigned long            SetLogicalLength ( unsigned long );
  196.     virtual    unsigned long            GetLogicalLength () const;
  197.  
  198.     virtual    const void*                GetPhysicalStart () const;
  199.     virtual    unsigned long            SetPhysicalLength ( unsigned long );
  200.     virtual    unsigned long            GetPhysicalLength () const;
  201.  
  202.     virtual DescType                GetDataType() const;
  203.     virtual void                    SetDataType(DescType dataType);
  204.     
  205.     virtual    void                    SetSource ( ADataItem* );
  206.             const ADataItem*            GetSource () const;
  207.  
  208.     virtual    void                    SetUpdate ( Boolean );
  209.             Boolean                    GetUpdate () const;
  210.     virtual    unsigned long            UpdateNow ();
  211.  
  212.     virtual    ostream&                operator >> ( ostream& ) const;
  213.  
  214. protected:    void                    UpdatePrefixWithLength ( unsigned long logicalLength );
  215.  
  216. private:    CDataItem                fDataItem;
  217.             ADataItem*                fSource;
  218.             DataItemPrefix            fPrefix;
  219.             Boolean                    fUpdate;
  220. };
  221.  
  222. /*============================================================================
  223.  
  224.     CWrapperDataItem is a concrete DataItem class which wraps a non-object
  225.     chunk of nonrelocatable memory.
  226.  
  227. ----------------------------------------------------------------------------*/
  228.  
  229. class CWrapperDataItem : public ADataItem
  230. {
  231. public:        CWrapperDataItem ( void* source, unsigned long length, DescType dataType = typeWildCard );
  232.     virtual    ~CWrapperDataItem ();
  233.  
  234.     virtual    const void*                GetPhysicalStart () const;
  235.     virtual    unsigned long            SetPhysicalLength ( unsigned long );
  236.     virtual    unsigned long            GetPhysicalLength () const;
  237.  
  238.     virtual DescType                GetDataType() const;
  239.     virtual void                    SetDataType(DescType dataType);
  240.     
  241. private:    void*                    fSource;
  242.             unsigned long            fLength;
  243.             DescType                fDataType;
  244. };
  245.  
  246. /***********************************|****************************************/
  247.  
  248. inline ADataItem::operator const void* () const { return GetPhysicalStart (); }
  249. inline void ADataItem::ZeroDataItem () { FillDataItem ( 0 ); }
  250. inline Boolean ADataItem::operator != ( const ADataItem& that ) const { return !operator == ( that ); }
  251.  
  252. inline const void* CDataItem::GetPhysicalStart () const { return fIsExternal ? fType.fExternal.fDataItem : fType.fInternal.fDataItem; }
  253. inline unsigned long CDataItem::GetPhysicalLength () const { return fIsExternal ? fType.fExternal.fLength : fType.fInternal.fLength; }
  254. inline unsigned long CDataItem::GetDataType ( ) const { return fDataType; }
  255. inline void CDataItem::SetDataType ( DescType dataType ) { fDataType = dataType; }
  256.  
  257. inline const void* CPrefixDataItem::GetPhysicalStart () const { return fDataItem.GetPhysicalStart (); }
  258. inline const void* CPrefixDataItem::GetLogicalStart () const { return (char*) fDataItem.GetPhysicalStart () + fPrefix; }
  259. inline unsigned long CPrefixDataItem::GetPhysicalLength () const { return fDataItem.GetPhysicalLength (); }
  260. inline unsigned long CPrefixDataItem::SetPhysicalLength ( unsigned long length ) { return fDataItem.SetPhysicalLength ( length ); }
  261. inline unsigned long CPrefixDataItem::GetDataType ( ) const { return typeWildCard; }
  262. inline void CPrefixDataItem::SetDataType ( DescType  ) { }
  263. inline void CPrefixDataItem::SetSource ( ADataItem* source ) { fSource = source; }
  264. inline const ADataItem* CPrefixDataItem::GetSource () const { return fSource; }
  265. inline void CPrefixDataItem::SetUpdate ( Boolean update ) { fUpdate = update; }
  266. inline Boolean CPrefixDataItem::GetUpdate () const { return fUpdate; }
  267.  
  268. inline const void* CWrapperDataItem::GetPhysicalStart () const { return fSource; }
  269. inline unsigned long CWrapperDataItem::GetPhysicalLength () const { return fLength; }
  270. inline unsigned long CWrapperDataItem::GetDataType ( ) const { return fDataType; }
  271. inline void CWrapperDataItem::SetDataType ( DescType dataType ) { fDataType = dataType; }
  272.  
  273. /***********************************|****************************************/
  274.  
  275. #pragma pop
  276.  
  277. #endif    // __DATAITEM__
  278.